﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InstaGM
{
    public partial class db_settings : Form
    {
        public db_settings()
        {
            InitializeComponent();
            ToolTip tooltip = new ToolTip();
            tooltip.ToolTipIcon = ToolTipIcon.Info;
            tooltip.ToolTipTitle = "Information";
            tooltip.IsBalloon = true;
            tooltip.ShowAlways = true;
            tooltip.SetToolTip(sa_q, "sa stands for \"System Administrator\"\n\nUse of \"sa\" IS NOT RECOMMENDED!!!\n\nExcepting only when using InstaGM to connect to your LOCAL database.");
            tooltip.SetToolTip(sa_q2, "sa stands for \"System Administrator\"\n\nUse of \"sa\" IS NOT RECOMMENDED!!!\n\nExcepting only when using InstaGM to connect to your LOCAL database.");
        
        }

        private void db_settings_Load(object sender, EventArgs e)
        {
            db_name.Text = Properties.Settings.Default.db_name;
            db_user.Text = Properties.Settings.Default.db_user;
            db_pass.Text = Properties.Settings.Default.db_pass;
     
        }

        private void instagm_db_setting_save_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.db_name = db_name.Text;
            Properties.Settings.Default.db_user = db_user.Text;
            Properties.Settings.Default.db_pass = db_pass.Text;

            Properties.Settings.Default.Save();
            Application.Restart();
        }

        private void test_telecaster_con_btn_Click(object sender, EventArgs e)
        {
            string host_val = Properties.Settings.Default.host;
            string connectionString = "server=" + host_val + "," + Properties.Settings.Default.db_port +";database=" + db_name.Text + ";user id=" + db_user.Text + ";password=" + db_pass.Text + ";Trusted_Connection=no;Timeout=10;"; 
            using (SqlConnection sqlcon = new SqlConnection(connectionString))
            {
                MessageBox.Show("Server IP: "+host_val+"\nDatabase Port: "+ Properties.Settings.Default.db_port +"\nDatabase Name: "+db_name.Text+"\nDatabase User: "+db_user.Text+"\nDatabase Password "+db_pass.Text+"");
                try
                {
                    sqlcon.Open();
                }
                catch (SqlException ex)
                {
                    StringBuilder errorMessages = new StringBuilder();

                    for (int i = 0; i < ex.Errors.Count; i++)
                    {
                        errorMessages.Append("Message: " + ex.Errors[i].Message + "");
                        MessageBox.Show(errorMessages.ToString(), "Error");                       
                    }
                }
            }
        }
    }
}
